<!--
 * @Descripttion:
 * @version:
 * @Author: cxguo
 * @Date: 2019-10-30 21:29:17
 * @LastEditors: cxguo
 * @LastEditTime: 2020-11-06 14:20:16
 -->
<template>
  <div>
    <!-- dataObj: {{ dataObj }} -->
    <div class="clearfix">
      <h3 class="fl">商品信息</h3>
    </div>
    <el-divider />
    <el-form
      ref="goodBase"
      :model="dataObj"
      :label-position="'right'"
      :rules="ruleValidator"
      label-width="60px"
    >
      <el-row :gutter="20">
        <el-col :span="12">
          <el-form-item label="名称:" prop="name">
            <el-input v-model="dataObj.name" />
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="分类:" prop="catId">
            <cat-tree-select v-model="dataObj.catId" />
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="商品状态" prop="isEnable">
            <el-switch
              v-model="dataObj.isEnable"
              active-value="1"
              inactive-value="0"
            />
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
  </div>
</template>
<script>
import { oftenuse } from '@/libs/mixins'
import { genGoodSpuCode } from '@/api/goods/goods-spu.js'
import CatTreeSelect from '@/views/components/GoodsCatTreeSelect'

export default {
  name: 'GoodBase',
  components: { CatTreeSelect },
  mixins: [oftenuse],
  props: {
    multi: {
      type: Object,
      default: () => { return {} }
    }
  },
  data() {
    return {
      dataObj: { catId: '00', catName: '默认分类', isEnable: '1' },
      ruleValidator: {
        name: [
          { required: true, message: '请输入商品名称', trigger: 'blur' },
          { min: 2, max: 30, message: '长度在 2 到 30 个字符', trigger: 'blur' }
        ],
        catId: [{ required: true, message: '请选择所属类别', trigger: 'change' }],
        catName: [{ required: true, message: '请选择所属类别', trigger: 'change' }],
        unit: [{ required: true, message: '请选择单位', trigger: 'change' }],
        specs: [{ required: true, message: '请输入规格', trigger: 'blur' }]
      }
    }
  },
  watch: {
  },
  created() {
    this.initData()
  },
  methods: {
    // 对外api
    setData(data) {
      this.dataObj = data
    },
    getData() {
      return this.dataObj
    },
    btnSelectType() {
      this.dialogVisible = true
    },
    // API
    resetData() {
      this.dataObj = {}
      this.$set(this.multi, 'specs', false)
      this.$set(this.multi, 'unit', false)
      this.initCode()
    },
    initCode() {
      genGoodSpuCode().then(res => {
        if (res.data.flag) {
          this.$set(this.dataObj, 'code', res.data.data)
        } else {
          this.$message.error('获取商品编号失败！')
        }
      })
    },
    initData() {
      this.initCode()
    }
  }
}
</script>
